#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::endl;
using std::cerr;
using std::cin;
using std::ofstream;
using std::ifstream;
using std::fstream;
using std::string;

void test()
{
    //文件输出流而言，当文件不存在的时候，就创建文件
    //当文件存在的时候，文件输出流会将文件清空
    ofstream ofs("wd.txt");
    if(!ofs.good())
    {
        cerr << "ofstream is not good" << endl;
        return;
    }

    ifstream ifs("PointTmp.cc");
    if(!ifs.good())
    {
        cerr << "ifstream is not good" << endl;
        return;
    }

    string line;
    while(getline(ifs, line))
    {
        ofs << line << endl;
    }

    ofs.close();
    ifs.close();
}

void test2()
{
    //对于文件输入输出流而言，当文件不存在的时候，就会报错，
    //所以文件必须要存在
    fstream fs("text.txt");
    if(!fs)
    {
        cerr << "fstream is not good" << endl;
        return;
    }

    //利用fs的输出特性，从键盘输入五个数据存到text.txt中
    int number = 0;
    for(size_t idx = 1; idx != 6; ++idx)
    {
        cin >> number;
        fs << number << " ";
    }

    //使用tellg/tellp查看文件对象的位置
    //g = get    p = put
    cout << "fs.tellg() = " << fs.tellg() << endl;
    cout << "fs.tellp() = " << fs.tellp() << endl;

    //文件流对象的偏移操作（文件指针的偏移）
    //seekp/seekg
    /* fs.seekg(0); */
    fs.seekg(0,std::ios::beg);

    //利用fs的输入特性，从文件text.txt中读出到屏幕上
    for(size_t idx = 1; idx != 6; ++idx)
    {
    /* cout << "fs.failbit = " << fs.fail() << endl */
    /*      << "fs.eofbit = " << fs.eof() << endl */
    /*      << "fs.goodbit = " << fs.good() << endl; */
        fs >> number;
        cout << number << " ";
    }
    fs.close();
}

void test3()
{
    ifstream ifs("PointTmp.cc", std::ios::ate);//文件的追加模式
    if(!ifs.good())
    {
        cerr << "ifstream is not good" << endl;
        return ;
    }

    cout << "ifs.tellg() = " << ifs.tellg() << endl;

    ifs.close();//关闭文件

}

void test4()
{
    ofstream ofs("wd.txt", std::ios::app);
    if(!ofs.good())
    {
        cerr << "ofstream is not good" << endl;
        return;
    }

    cout << "ofs.tellp() = " << ofs.tellp() << endl;

    ofs.close();
}
int main(int argc, char **argv)
{
    test4();
    return 0;
}

